<template>
    <view>
        <view v-if="!isLoad" class="margin-top load loading text-l text-admin"></view>

        <view class="main-admin" v-if="isAdmin && isLoad">
            <view class="form-box shadow">
                <view class="form-group">
                    <view class="title text-cut">{{ title }}</view>
                </view>
            </view>

            <view class="form-box shadow margin-top-xs">
                <view class="checkin" @tap="bindScanTap">
                    <view class="notice">
                        <text class="icon-scan margin-right-s text-bold"></text>
                        扫码核销
                    </view>
                    <view class="desc">管理员扫描用户预约码进行签到核销</view>
                    <view class="oprt">立即核销</view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
import cmptPickerTime from '../../../../cmpts/public/picker_time/picker_time_cmpt';
const AdminBiz = require('../../../../biz/admin_biz.js');
const pageHelper = require('../../../../helper/page_helper.js');
const cloudHelper = require('../../../../helper/cloud_helper.js');
export default {
    components: {
        cmptPickerTime
    },
    data() {
        return {
            isLoad: true,
            title: '',
            isAdmin: ''
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (!AdminBiz.isAdmin(this)) {
            return;
        }
        if (!pageHelper.getOptions(this, options, 'meetId')) {
            return;
        }
        if (options && options.title) {
            let title = decodeURIComponent(options.title);
            this.setData({
                title
            });
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    methods: {
        bindScanTap: function (e) {
            let meetId = this.meetId;
            uni.scanCode({
                async success(res) {
                    console.log(res);
                    if (!res || !res.result || !res.result.includes('meet=') || res.result.length != 20) {
                        pageHelper.showModal('错误的预约码，请重新扫码');
                        return;
                    }
                    let code = res.result.replace('meet=', '');
                    let params = {
                        meetId,
                        code
                    };
                    let options = {
                        title: '预约码核销中'
                    };
                    await cloudHelper
                        .callCloudSumbit('admin/join_scan', params, options)
                        .then((res) => {
                            pageHelper.showModal('核销成功');
                        })
                        .catch((err) => {
                            console.log(err);
                        });
                },
                fail(err) {
                    if (err && err.errMsg == 'scanCode:fail') {
                        pageHelper.showModal('预约码核销错误，请重新扫码');
                    }
                }
            });
        }
    }
};
</script>
<style>
@import './admin_meet_scan.css';
</style>
